class_name StateMachine
extends Node

# 新的状态机控制器 - 修复拼写错误并重构架构

signal state_changed(from_state: String, to_state: String)
signal state_entered(state_name: String)
signal state_exited(state_name: String)

@export var character: Character
@export var animation_player: AnimationPlayer
@export var debug_mode: bool = false

var states: Dictionary = {}
var current_state: State
var previous_state_name: String = ""
var current_state_name: String = ""

# 初始化状态机
func _ready() -> void:
	if not character:
		character = owner as Character
	
	

# 注册状态
func add_state(state_name: String, state: State) -> void:
	if states.has(state_name):
		push_warning("State '%s' already exists, replacing..." % state_name)
		if states[state_name] == current_state:
			current_state = null
		states[state_name].queue_free()
	
	states[state_name] = state
	state.initialize(self, character, state_name, animation_player)
	add_child(state)
	
	if debug_mode:
		print("Added state: %s" % state_name)

# 移除状态
func remove_state(state_name: String) -> void:
	if not states.has(state_name):
		push_warning("State '%s' does not exist" % state_name)
		return
	
	if current_state == states[state_name]:
		push_error("Cannot remove current active state '%s'" % state_name)
		return
	
	states[state_name].queue_free()
	states.erase(state_name)

# 改变状态
func change_state(new_state_name: String, data: Dictionary = {}) -> bool:
	if not states.has(new_state_name):
		push_error("State '%s' does not exist" % new_state_name)
		return false
	
	# 检查当前状态是否允许转换
	if current_state and not current_state.can_transition_to(new_state_name):
		if debug_mode:
			print("Transition from '%s' to '%s' not allowed" % [current_state_name, new_state_name])
		return false
	
	var old_state_name = current_state_name
	
	# 退出当前状态
	if current_state:
		current_state.exit()
		state_exited.emit(current_state_name)
		if debug_mode:
			print("Exited state: %s" % current_state_name)
	
	# 更新状态引用
	previous_state_name = current_state_name
	current_state_name = new_state_name
	current_state = states[new_state_name]
	
	# 进入新状态
	current_state.enter(data)
	state_entered.emit(current_state_name)
	state_changed.emit(old_state_name, current_state_name)
	
	if debug_mode:
		print("Entered state: %s" % current_state_name)
	
	return true

# 启动状态机
func start(initial_state: String, data: Dictionary = {}) -> bool:
	if states.is_empty():
		push_error("No states registered in state machine")
		return false
	
	return change_state(initial_state, data)

# 获取当前状态
func get_current_state() -> State:
	return current_state

# 获取当前状态名称
func get_current_state_name() -> String:
	return current_state_name

# 获取上一个状态名称
func get_previous_state_name() -> String:
	return previous_state_name

# 检查是否在指定状态
func is_in_state(state_name: String) -> bool:
	return current_state_name == state_name

# 获取所有状态名称
func get_all_state_names() -> Array[String]:
	var names: Array[String] = []
	for _name in states.keys():
		names.append(_name)
	return names

# 更新当前状态
func _process(delta: float) -> void:
	if current_state:
		current_state.update(delta)

# 物理更新当前状态
func _physics_process(delta: float) -> void:
	if current_state:
		current_state.physics_update(delta)

# 获取调试信息
func get_debug_info() -> Dictionary:
	return {
		"current_state": current_state_name,
		"previous_state": previous_state_name,
		"total_states": states.size(),
		"state_details": current_state.get_debug_info() if current_state else "No active state"
	}

# 强制停止状态机
func stop() -> void:
	if current_state:
		current_state.exit()
		state_exited.emit(current_state_name)
	
	current_state = null
	current_state_name = ""
	previous_state_name = ""
